/***************************************************************************//**
 * @file FDM/FDM_Main.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2014 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef FDM_MAIN_CPP
#define FDM_MAIN_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <FDM/FDM_Main.h>

////////////////////////////////////////////////////////////////////////////////

using namespace std;

////////////////////////////////////////////////////////////////////////////////

FDM_Main::FDM_Main() :
    m_fdm  ( 0 ),

    m_ctrl ( 0 ),
    m_envr ( 0 ),
    m_rcrd ( 0 ),

    m_timeStepSim ( 0.0 ),
    m_timeStepInt ( 0.0 ),

    m_timeIntSteps ( 0 )
{
    m_fdm = new MSc_FDM();

    m_ctrl = new FDM_Controls();
    m_envr = new FDM_Environment();
    m_rcrd = new FDM_Recording();
}

////////////////////////////////////////////////////////////////////////////////

FDM_Main::~FDM_Main()
{
    if ( m_ctrl ) delete m_ctrl; m_ctrl = 0;
    if ( m_envr ) delete m_envr; m_envr = 0;
    if ( m_rcrd ) delete m_rcrd; m_rcrd = 0;

    if ( m_fdm ) delete m_fdm; m_fdm = 0;
}

////////////////////////////////////////////////////////////////////////////////

void FDM_Main::init( const FDM_DataInp &/*dataInp*/, FDM_DataOut &/*dataOut*/ )
{
    //m_fdm->initBuffer();
}

////////////////////////////////////////////////////////////////////////////////

void FDM_Main::update( double timeStep, const FDM_DataInp &/*dataInp*/, FDM_DataOut &/*dataOut*/ )
{
    computeTimeStep( timeStep );

    anteLoop();

    for ( int i = 0; i < m_timeIntSteps; i++ )
    {
        anteStep();

        //////////////////////////
        m_fdm->update( timeStep );
        //////////////////////////

        postStep();
    }

    postLoop();
}

////////////////////////////////////////////////////////////////////////////////

void FDM_Main::anteLoop() {}

////////////////////////////////////////////////////////////////////////////////

void FDM_Main::anteStep()
{
//    m_envr->update();
}

////////////////////////////////////////////////////////////////////////////////

void FDM_Main::postStep() {}

////////////////////////////////////////////////////////////////////////////////

void FDM_Main::postLoop() {}

////////////////////////////////////////////////////////////////////////////////

void FDM_Main::computeTimeStep( double timeStep )
{
    // checking time step limits
    if ( timeStep < MSCSIM_TIME_STEP_MIN )
    {
        m_timeStepSim = MSCSIM_TIME_STEP_MIN;
        cerr << " FDM Warning: time step = " << timeStep << " s" << endl;
    }
    else if ( timeStep > MSCSIM_TIME_STEP_MAX )
    {
        m_timeStepSim = MSCSIM_TIME_STEP_MAX;
        cerr << " FDM Warning: time step = " << timeStep << " s" << endl;
    }
    else
    {
        m_timeStepSim = timeStep;
    }

    // computing constant time step
    m_timeIntSteps = (int)floor( m_timeStepSim / MSCSIM_TIME_STEP_INT + 0.5 );

    if ( m_timeIntSteps < 1 ) m_timeIntSteps = 1;

    m_timeStepInt = m_timeStepSim / (double)m_timeIntSteps;
}
